/**
 * File: Plan.java
 * Project: CMSC 345 - VirtualAdvisor
 * @author Wallace Brown
 * Date: April 26, 2011
 * E-mail: brownw1@umbc.edu 
 * Description: The class maintaining user's semesters.
 */
package virtualadvisor;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;


public class Plan{

	private List<Semester> semesters;
	private String name;

	/**
	 * Name: Plan
	 * Precondition: None.
	 * Postcondition: Returns a plan object.
	 */
	public Plan(){
		semesters = new ArrayList<Semester>();
		name = "";
	}
	
	/**
	 * Name: Plan
	 * Precondition: None.
	 * Postcondition: Returns a plan object.
	 */
	public Plan(String name) { 
		this.semesters = new LinkedList<Semester>();
		this.name = name; 
	}
	
	public Plan(List<Semester> semesters, String name){
		this.semesters = semesters;
		this.name = name; 

		Collections.sort(semesters, new SemesterComparator());
	}
	
	/**
	 * Name: AddSemester
	 * Precondition: None.
	 * Postcondition: We have added a semester to our plan.
	 * 		The plan's semesters are sorted.
	 */
	public void addSemester(Semester semester) {
		if(!semesters.contains(semester)) 
			semesters.add(semester);
		
		Collections.sort(semesters, new SemesterComparator());
	}
	
	/**
	 * Name: DeleteSemester
	 * Precondition: None.
	 * Postcondition: We have deleted a semester from our plan.
	 */
	public void deleteSemester(Semester semester) { semesters.remove(semester); }

	
	public boolean contains(Course c){
		for(int i = 0; i < semesters.size(); i++)
		{
			if(semesters.get(i).contains(c))
				return true;
		}
		return false;
	}
	/**
	 * Name: getSemester
	 * Precondition: None.
	 * Postcondition: We have returned the requested semester.
	 */
	public Semester getSemester(String season, int year) {
		Iterator<Semester> it = semesters.iterator();
		while(it.hasNext()){
			Semester tmp = it.next();
			if(tmp.getSeason().equals(season) && tmp.getYear() == year)
				return tmp;
		}
		return null;
	}
	
	/**
	 * Name: getName
	 * Precondition: None.
	 * Postcondition: We have returned the plan name.
	 */
	public String getName() { return name; }
	
	/**
	 * Name: getName
	 * Precondition: None.
	 * Postcondition: We have returned the plan name.
	 */
	public void setName(String name) { this.name = name; }
	
	/**
	 * Name: numberOfSemesters()
	 * Precondition: None
	 * Postcondition: Number of semesters
	 * @return - Number of semesters.
	 */
	public int numberOfSemesters() { return semesters.size(); }

	/**
	 * Name: getTermYear
	 * Precondition: None
	 * Postcondition: Gets the season and year for the specific semester
	 * @param number - Semester to look at
	 * @return - Season and year
	 */
	public String getTermYear(int number){
		return semesters.get(number).getSeasonYear();
	}
	
	/**	[DBG-DBG-DBG-DBG-DBG-DBG]
	 * Name: getList
	 * Precondition: None
	 * Postcondition: Gets the list of semesters.
	 * @return - list of semesters
	 */
	public List<Semester> getList(){return semesters;}
	
	/**
	 * Name: XMLCoder
	 * Precondition: None
	 * Postcondition: Gathers information on class to be displayed 
	 *              in XML format for saving to file.
	 * @return - Output on the class to be displayed in XML format
	 */
	public String XMLCoder() {
		String xml = "";
		
	    for(int x = 0; x < semesters.size(); x++) {
	    	xml = "  <planned term=\"" + semesters.get(x).getSeason() + " " + semesters.get(x).getYear() + "\">\n";
	    	xml = xml + semesters.get(x).XMLCoder();
	    } 
	      xml = xml + "   </planned>\n";
	      
		return xml;
	}
	
	public String toString(){
		String text = "";
		text = name + "\n";
		for(int x = 0; x < semesters.size(); x++){
			text = text + semesters.get(x).toString();
		}
		return text;
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		LinkedList<String> myPre = new LinkedList<String>();
		myPre.add("MATH150");
		
		Course c = new Course("CMSC201", "CMSC", 201, "COMPUTERS", "Computer Stuff", ' ', 4, myPre, false, null);
		Semester s = new Semester("Fall", 2010);
		s.addCourse(c);
		Plan p = new Plan("CMSC");
		p.addSemester(s);
		System.out.println(p.XMLCoder());
	}
}
